package com.cat.slidingWindow;

/**
 * @author cat
 * @description https://leetcode.cn/problems/maximize-the-confusion-of-an-exam/description/
 * @create 2025/7/29 14:27
 * @since JDK17
 */

public class Solution09 {
    public int maxConsecutiveAnswers(String answerKey, int k) {
        char[] s = answerKey.toCharArray();
        // 反正就两种选择
        return Math.max(f(s, k, 'T'), f(s, k, 'F'));
    }

    public int f(char[] s, int k, char target) {
        int ans = 0, n = s.length, cnt = k;
        for (int l = 0, r = 0; r < n; r++) {
            cnt -= s[r] != target ? 1 : 0;  // 转一次
            while (cnt < 0) {   // 多转了
                cnt += s[l++] != target ? 1 : 0;  // 吐出多转的
            }
            ans = Math.max(ans, r - l + 1);
        }
        return ans;
    }
}
